/**
 * Created by YJH on 2018/5/30.
 */

import React, {Component} from 'react';
import {
    AsyncStorage,
} from 'react-native';
import ThemeFactory, {ThemeFlags} from '../../../res/styles/ThemeFactory';
const THEME_KEY = 'themeKey_';
export default class ThemeDao {


    /**
     * 功能：保存主题到数据库
     * @param theme
     */
    saveTheme(theme){
        AsyncStorage.setItem(THEME_KEY, theme, error=>{})
    }

    getTheme(){
        // AsyncStorage.removeItem(THEME_KEY);
        return new Promise((resolve, reject)=>{
            AsyncStorage.getItem(THEME_KEY,(error,result)=>{
                if(error){
                    reject(error);
                    return;
                }

                if(!result){
                    //用户第一次启动，没有切换过主题，给你一个默认的
                    this.saveTheme(ThemeFlags.Blue);
                    result=ThemeFlags.Blue;
                }
                resolve(ThemeFactory.createTheme(result));
            })
        });
    }

}